#include <my_header.h>

/* Usage:  */
int main(int argc, char *argv[]){                                  
    int pipe_fd = open("shared.pipe", O_RDWR);
    ERROR_CHECK(pipe_fd, -1, "open");

    fd_set set_read;
    fd_set set_write;
    // 初始化文件描述符
    FD_ZERO(&set_read);
    FD_ZERO(&set_write);

    while(1)
    {
        // 将文件描述符添加到fd_set集合中
        FD_SET(pipe_fd, &set_read);
        FD_SET(pipe_fd, &set_write);
        select(10, &set_read, &set_write, NULL, NULL);
        
        // 写入4096 并判断文件描述符是否就绪
        if(FD_ISSET(pipe_fd, &set_write))
        {
            char buf[4096] = {0};
            write(pipe_fd, buf, sizeof(buf));
            printf("write once \n");
        }
        // 读取1024 并判断文件描述符是否就绪
        if(FD_ISSET(pipe_fd, &set_read))
        {
            char buf[1024] = {0};
            read(pipe_fd, buf, sizeof(buf));
            printf("read once \n");
        }
        sleep(1);
    }


    close(pipe_fd);
    return 0;
}


